rm(list=ls())
library(plyr);library(dplyr)
library(tidyr)
library(igraph)
library(ggplot2)
library(stargazer)

#setwd('')

dat = readRDS('/Users/jeremyspater/Dropbox/duke/wibbels RA/segregation paper/network/3-6-17 jaipat deid.RDS')

#put caste in own category
dat$C.C8_Caste = mapvalues(dat$C.C8_Caste, from = c('BC', 'General', 'OBC', 'SC', 'ST'),
                           to = c('General/BC/OBC','General/BC/OBC','General/BC/OBC','SC/ST','SC/ST'))

#recode neighborhood values
dat$A.A7 = mapvalues(dat$A.A7, from = c("SM00042", "SM00051", "SM00082", "SM00093"),
                     to = c("42", "51", "82", "93"))
dat$A.A7 = paste(dat$City, dat$A.A7)  

dat$F.F1_Monthly.Income = mapvalues(dat$F.F1_Monthly.Income, from = c(-888, 888, 999),
                                    to = c(NA, NA, NA))
##########################################################################################################################

###########################################################################################################################
#Make "graphs"

#Social
GrSoc1 = function(x){
  el_bor = cbind(x$code3,as.character(x$G.G2.G2B_Code2))
  el_bor2 = cbind(x$code3,as.character(x$G.G3.G3B_Code2))
  el_vis = cbind(x$code3,as.character(x$I.I7.I7B_Code2))
  
  el = rbind(el_bor, el_bor2, el_vis)
  el[is.na(el[,2]),2] = el[is.na(el[,2]),1] 
  
  gr = graph_from_edgelist(el,directed=TRUE) %>% simplify(remove.multiple=FALSE, remove.loops=TRUE) 
  
  V(gr)$Religion=as.character(dat$C.C6[match(V(gr)$name,dat$code3)])
  V(gr)$Caste=as.character(dat$C.C8_Caste[match(V(gr)$name,dat$code3)])
  V(gr)$Neighborhood = x$A.A7[1] 
  return(gr)
}

#Political
GrPol1 = function(x){
  el_ser = cbind(x$code3,as.character(x$I.I3_Group.I3B_Code2)) #help getting service
  el_meet = cbind(x$code3,as.character(x$I.I16.I16_Lead.meeting)) 
  el_vote = cbind(x$code3,as.character(x$I.I33B_Code2)) #important in how you vote
  el_lea = cbind(x$code3,as.character(x$I.I25B_Code2)) #leader
  
  el = rbind(el_ser, el_vote, el_lea)
  el[is.na(el[,2]),2] = el[is.na(el[,2]),1] #turn non-links into loops
  
  gr = graph_from_edgelist(el,directed=TRUE) %>% simplify(remove.multiple=FALSE, remove.loops=TRUE) #eliminate loops
  V(gr)$Religion=as.character(dat$C.C6[match(V(gr)$name,dat$code3)])
  V(gr)$Caste=as.character(dat$C.C8_Caste[match(V(gr)$name,dat$code3)])
  V(gr)$Neighborhood = x$A.A7[1] 
  return(gr)
}

#Economic
GrEcon1 = function(x){
  el_job = cbind(x$code3,as.character(x$D.D4.D4B_Code2)) #help getting job
  el_work = cbind(x$code3,as.character(x$D.D8.D8B_Code2)) #work with
  el_unemp = cbind(x$code3,as.character(x$E.E3B_Code2)) #unable to find work
  el_house = cbind(x$code3,as.character(x$H.H7.H7B_Code2)) #sell house
  
  el = rbind(el_job, el_work, el_unemp, el_house)
  el[is.na(el[,2]),2] = el[is.na(el[,2]),1] #turn nonlinks into loops
  
  gr = graph_from_edgelist(el,directed=TRUE) %>% simplify(remove.multiple=FALSE, remove.loops=TRUE) #remove loops
  V(gr)$Religion=as.character(dat$C.C6[match(V(gr)$name,dat$code3)])
  V(gr)$Caste=as.character(dat$C.C8_Caste[match(V(gr)$name,dat$code3)])
  V(gr)$Neighborhood = x$A.A7[1] 
  return(gr)
}


#All Together
GrAll = function(x){
  
  #social
  el_bor = cbind(x$code3,as.character(x$G.G2.G2B_Code2))
  el_bor2 = cbind(x$code3,as.character(x$G.G3.G3B_Code2))
  el_vis = cbind(x$code3,as.character(x$I.I7.I7B_Code2))
  
  #political
  el_ser = cbind(x$code3,as.character(x$I.I3_Group.I3B_Code2)) #help getting service
  el_meet = cbind(x$code3,as.character(x$I.I16.I16_Lead.meeting)) 
  el_vote = cbind(x$code3,as.character(x$I.I33B_Code2)) #important in how you vote
  el_lea = cbind(x$code3,as.character(x$I.I25B_Code2)) #leader
  
  #economic
  el_job = cbind(x$code3,as.character(x$D.D4.D4B_Code2)) #help getting job
  el_work = cbind(x$code3,as.character(x$D.D8.D8B_Code2)) #work with
  el_unemp = cbind(x$code3,as.character(x$E.E3B_Code2)) #unable to find work
  el_house = cbind(x$code3,as.character(x$H.H7.H7B_Code2)) #sell house
  
  el = rbind(el_bor, el_bor2, el_vis, el_ser, el_vote, el_lea, el_job, el_work, el_unemp, el_house)
  el[is.na(el[,2]),2] = el[is.na(el[,2]),1] #turn nonlinks into loops (include isolates)
  
  gr = graph_from_edgelist(el,directed=TRUE) %>% simplify(remove.multiple=FALSE, remove.loops=TRUE) #remove loops
  V(gr)$Religion=as.character(dat$C.C6[match(V(gr)$name,dat$code3)])
  V(gr)$Caste=as.character(dat$C.C8_Caste[match(V(gr)$name,dat$code3)])
  V(gr)$Neighborhood = x$A.A7[1] 
  return(gr)
}

#create "graphs" for each set of questions
SocList1 = dlply(dat, 'A.A7', function(x) GrSoc1(x))
PolList1 = dlply(dat, 'A.A7', function(x) GrPol1(x))
EconList1 = dlply(dat, 'A.A7', function(x) GrEcon1(x))
AllList1 = dlply(dat, 'A.A7', function(x) GrAll(x))
###########################################################################################################################

###########################################################################################################################
indeg = function(x){
  df = data.frame(code3 = names(V(x)),
                  in_degree = degree(x, mode = 'in',
                                     loops = TRUE, normalized = FALSE))
}

outdeg = function(x){
  df = data.frame(code3 = names(V(x)),
                  out_degree = degree(x, mode = 'out',
                                     loops = TRUE, normalized = FALSE))
}

all_indeg = llply(AllList1, function(x) indeg(x))
all_outdeg = llply(AllList1, function(x) outdeg(x))

all_in_df = do.call(rbind, all_indeg)
all_out_df = do.call(rbind, all_outdeg)

dat$all_in = all_in_df[match(dat$code3, all_in_df$code3),'in_degree']
dat$all_out = all_out_df[match(dat$code3, all_out_df$code3),'out_degree']
dat$all_combined = dat$all_in + dat$all_out
###########################################################################################################################

###########################################################################################################################
dat$I6 = as.numeric(as.character(dat$I.I6_Community.work))
dat$I10 = mapvalues(dat$I.I10.Disagreement, from = c(888, 999), to = c(NA, NA)) %>% as.numeric(as.character())
dat$I15 = mapvalues(dat$I.I15_Community.meeting.last.12.months, from = c(888, 999), to = c(NA, NA)) %>% as.numeric(as.character())
###########################################################################################################################

###########################################################################################################################
#Table D1
a = lm('all_in ~ I6',data = dat)
b = lm('all_in ~ I6 + A.A7',data = dat)

c = lm('all_out ~ I6',data = dat)
d = lm('all_out ~ I6 + A.A7',data = dat)

e = lm('all_combined ~ I6',data = dat) 
f = lm('all_combined ~ I6 + A.A7',data = dat) 

stargazer(a,b,c,d,e,f,
          omit = c('A.A7'),                        
          omit.labels = c("Neighborhood dummies?"),
          omit.stat = c("f", "ser"),
          dep.var.labels   = c('In-degree',
                               'Out-degree',
                               'Total-degree'),
          covariate.labels = c('Community Work','Constant'))

rm(a,b,c,d,e,f)

#this output must be manually edited in latex to: change dummies variable; change dep var labels

#####
#Table D2
a = lm('all_in ~ I10',data = dat)
b = lm('all_in ~ I10 + A.A7',data = dat)
c = lm('all_out ~ I10',data = dat)
d = lm('all_out ~ I10 + A.A7',data = dat)
e = lm('all_combined ~ I10',data = dat)
f = lm('all_combined ~ I10 + A.A7',data = dat)
  
stargazer(a,b,c,d,e,f,
          omit = c('A.A7'),                        
          omit.labels = c("Neighborhood dummies?"),
          omit.stat = c("f", "ser"),
          dep.var.labels   = c('In-degree',
                               'Out-degree',
                               'Total-degree'),
          covariate.labels = c('Neighborhood Disagreements','Constant'))

rm(a,b,c,d,e,f)

#####
#Table D3
a = lm('all_in ~ I15',data = dat)
b = lm('all_in ~ I15 + A.A7',data = dat)
c = lm('all_out ~ I15',data = dat)
d = lm('all_out ~ I15 + A.A7',data = dat)
e = lm('all_combined ~ I15',data = dat)
f = lm('all_combined ~ I15 + A.A7',data = dat)

stargazer(a,b,c,d,e,f,
          omit = c('A.A7'),                        
          omit.labels = c("Neighborhood dummies?"),
          omit.stat = c("f", "ser"),
          dep.var.labels   = c('In-degree',
                               'Out-degree',
                               'Total-degree'),
          covariate.labels = c('Neighborhood Meeting','Constant'))

rm(a,b,c,d,e,f)